package jinyilw.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class ExecuteTools
{
	private static String javaBinPath;
	private ExecuteTools()
	{
	}
	public static String getJavaBinPath()
	{
		if (javaBinPath == null)
			javaBinPath = System.getProperty("java.home") + File.separator
					+ "bin" + File.separator + "java";
		return javaBinPath;
	}
	public static void exeJarFile(File file, String splashPicture)
	{
		if (file == null || !file.isFile())
			return;
		ArrayList<String> command = new ArrayList<>();
		command.add(getJavaBinPath());
		if (splashPicture != null)
			command.add("-splash:" + splashPicture);
		command.add("-jar");
		command.add(file.getPath());
		ProcessBuilder builder = new ProcessBuilder(command);
		try
		{
			builder.start();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public static void exeFile(File file)
	{
		if (file == null || !file.isFile())
			return;
		exec(file.getAbsolutePath());
	}
	public static void makeExecutable(File file)
	{
		if (file != null && file.isFile())
			System.out.println(execute("chmod a+x " + file.getAbsolutePath()));
	}
	public static void exeMultiCommands(String commandStr)
	{
		if (OSTools.isWindows())
			exeCmd(commandStr);
		else
			exeBash(commandStr);
	}
	public static void exeBash(String commandStr)
	{
		// exec("/bin/sh -c " + commandStr);
		ArrayList<String> command = new ArrayList<>();
		command.add("/bin/sh");
		command.add("-c");
		command.add(commandStr);
		ProcessBuilder builder = new ProcessBuilder(command);
		try
		{
			builder.start();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public static void exeCmd(String commandStr)
	{
		// exec("cmd /c " + commandStr);
		ArrayList<String> command = new ArrayList<>();
		command.add("cmd");
		command.add("/c");
		command.add(commandStr);
		ProcessBuilder builder = new ProcessBuilder(command);
		try
		{
			builder.start();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public static void exeCmdInNewWindow(String commandStr)
	{
		exec("cmd /k start " + commandStr);
	}
	public static void exeBatFile(String path)
	{
		exec("cmd /k start " + path);
	}
	public static void exeBatFile(File file)
	{
		if (file != null && file.isFile())
			exec("cmd /k start " + file.getPath());
	}
	public static void exeWhenExitApp(Thread thread)
	{
		if (thread != null)
			Runtime.getRuntime().addShutdownHook(thread);
	}
	public static void killWhenExitApp(Process process)
	{
		if (process != null)
			exeWhenExitApp(new ProcessKiller(process));
	}
	public static void exeWhenExitApp(Runnable runnable)
	{
		if (runnable != null)
			exeWhenExitApp(new Thread(runnable));
	}
	public static void exec(String commandStr)
	{
		try
		{
			Runtime.getRuntime().exec(commandStr);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	public static String execute(String commandStr)
	{
		BufferedReader br = null;
		try
		{
			Process p = Runtime.getRuntime().exec(commandStr);
			br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null)
				sb.append(line).append("\n");
			return sb.toString();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally
		{
			if (br != null)
				try
				{
					br.close();
				} catch (Exception e)
				{
					e.printStackTrace();
				}
		}
		return null;
	}

	static class ProcessKiller extends Thread
	{
		private final Process process;

		public ProcessKiller(Process process)
		{
			this.process = process;
		}

		@Override
		public void run()
		{
			process.destroy();
		}
	}
}
